package com.zhangyuji.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.entity.AddressBook;
import com.zhangyuji.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    /**
     * 添加地址
     *
     * @param addressBook
     * @param request
     * @return
     */

    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        log.info("addressBook={}", addressBook);
        String userPhone = (String) request.getSession().getAttribute("user");
        Long userId = addressBookService.getUserId(userPhone);
        addressBook.setUserId(userId);
        MyCurrentId.setCurrentId(userId);
        boolean flag = addressBookService.save(addressBook);
        return flag ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 查询地址信息
     *
     * @param request
     * @return
     */

    @GetMapping("/list")
    public R<List<AddressBook>> getList(HttpServletRequest request) {
        String userPhone = (String) request.getSession().getAttribute("user");
        List<AddressBook> addressBookList = addressBookService.getListByUserAndAddressBook(userPhone);
        return R.success(addressBookList);
    }

    /**
     * 查询默认地址
     *
     * @param request
     * @return
     */

    @GetMapping("/default")
    public R<AddressBook> getAddressBookDefault(HttpServletRequest request) {
        String userPhone = (String) request.getSession().getAttribute("user");
        Long userId = addressBookService.getUserId(userPhone);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDefault, 1)
                .eq(AddressBook::getUserId, userId);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return addressBook != null ? R.success(addressBook) : R.error("没有查询到默认地址");
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @param request
     * @return
     */

    @PutMapping("/default")
    @Transactional
    public R<AddressBook> setAddressBookDefault(@RequestBody AddressBook addressBook, HttpServletRequest request) {

        String userPhone = (String) request.getSession().getAttribute("user");
        Long userId = addressBookService.getUserId(userPhone);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId)
                .set(AddressBook::getIsDefault,0);

        addressBookService.update(wrapper);

        MyCurrentId.setCurrentId(Long.valueOf(userPhone));

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }


}
